package com.digiproto.app_store;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.digiproto.app_store.model.Game;

import java.util.ArrayList;

/**
 * Created by TreeNode on 2017/11/8.
 */

public class NewListFragment extends Fragment implements AdapterView.OnItemClickListener {
    // 管理器
    private FragmentManager fManager;
    // 数据
    private ArrayList<Game> datas;
    // 视图
    private ListView game_list;

    // 构造器
//    public NewListFragment(FragmentManager fManager, ArrayList<Game> datas) {
//        this.fManager = fManager;
//        this.datas = datas;
//    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        // 首先找到布局文件
        // View view = inflater.inflate(R.layout.index_list, container, false);
        // 然后找到listview视图
        // game_list = (ListView)view.findViewById(R.id.game_list);
        // GameList 适配器
        Bundle bundle = getArguments();
        if(null != bundle){
            this.fManager = (FragmentManager) bundle.get("fmanager");
            this.datas = (ArrayList<Game>) bundle.get("datas");
        }
        GameListAdapter gameListAdapter = new GameListAdapter(datas,getActivity());
        // 将适配器设置到listview上
        game_list.setAdapter(gameListAdapter);
        // 添加监听器
        game_list.setOnItemClickListener(this);
        // 返回view
        return null;
    }

    /**
     * 视图切换
     * @param parent
     * @param view
     * @param position
     * @param id
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // 开始碎片事务
        FragmentTransaction fTransaction = fManager.beginTransaction();
        // 创建游戏详情页Fragment对象
        // GameDetailFragment gdf = new GameDetailFragment();
        // 传递数据对象
        Bundle bddata = new Bundle();
        // 设置传递的数据

        // 将数据设置到参数上
        // gdf.setArguments(bddata);
        // 获取Activity控件

        // 加上Fragment替换动画
        // fTransaction.setCustomAnimations(R.anim.fragment_slide_left_enter,R.anim.fragment_slide_left_exit);
        // 替换
        // fTransaction.replace(R.id.vp_main, gdf);
        // 调用addToBackStack将Fragment添加到栈中
        fTransaction.addToBackStack(null);
        fTransaction.commit();
    }
}
